#!/bin/bash

#Change primary group, need reboot to take effect.
usermod -g root lambert

#Replace and update sources.list
sudo bash -c 'cat <<EOF > /etc/apt/sources.list
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
EOF'

#install common tools
sudo apt-get install -y net-tools vim curl gconf-editor

#install chrome
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list

sudo apt update
sudo apt install google-chrome-stable

#Install fcitx input method
#<GUI
Language Support -> Install simplied chinese and Japanese
#>

##Install fcitx
sudo apt install -y fcitx fcitx-bin fcitx-config-gtk fcitx-frontend-all fcitx-pinyin fcitx-table fcitx-googlepinyin fcitx-table-wbpy fcitx-anthy 

#Run to add /usr/bin/fcitx entry
gnome-session-properties
#<GUI>#

#Change input method to fcitx
#<GUI
# :: => Input Method (it's a keyboard icon) and selec fcitx
#>

#reboot the computer
reboot

#Add input methods
#<GUI
# :: => fcitx configuration, add input method by "+", remember to untick "Only Show Current Language", and the name is case-sensitive!!!
#>

#Logout to make the changes take effect
# CTRL + SPACE to activate the input method,
# Ctrl + Shift to switch between different methods.

#install zsh
sudo apt install -y terminator zsh git
sudo apt-get -y install zsh
#NOTE: interactive
sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"


#install zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh

# Always load zsh-autosuggestions.
bash -c 'cat <<EOF >> ~/.zshrc
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
EOF'

# install SDK man
#home: https://sdkman.io/install
curl -s "https://get.sdkman.io" | bash
#<GUI open a new terminal >
source "$HOME/.sdkman/bin/sdkman-init.sh"

#install JDK sdk 11.0.2-zulufx
sdk install java 11.0.2-zulufx

#install gradle
sdk install gradle 5.6.2

#install visual studio code
sudo apt update
sudo apt install -y software-properties-common apt-transport-https wget
wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
sudo apt update
sudo apt install -y code

#install intellij from ubuntu software center
# :: => Ubuntu Softwre, search "IDEA"

#Install gnome themes
sudo apt-get install -y gnome-tweaks
#refer to https://www.jianshu.com/p/49ed3971170a for details

#Install dconf-tools to change the looking of nautilus file manager
sudo apt-get install -y dconf-tools
dconf-editor &

#remove snap with apt packages
snap  remove gnome-calculator gnome-system-monitor gnome-logs gnome-characters 
sudo apt-get install -y gnome-calculator gnome-system-monitor gnome-logs gnome-characters

#install real-time CPU and network status
#Refer to: https://launchpad.net/~fossfreedom/+archive/ubuntu/indicator-sysmonitor
#! not working well...
sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor
sudo apt-get install indicator-sysmonitor

#install sublime text 3
wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -
echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list
sudo apt-get update
sudo apt-get install sublime-text
#consider following plugins: Packet Control, Emmet, SidebarEnhancements, SublimeLinter, BracketHighlighter, HTML5, Alignment

#install beyond compare
wget https://www.scootersoftware.com/bcompare-4.2.10.23938_amd64.deb
sudo apt-get update
sudo apt-get install gdebi-core
sudo gdebi bcompare-4.2.10.23938_amd64.deb
#activate beyond compare
# at https://gist.github.com/satish-setty/04e1058d3043f4d10e2d52feebe135e8
sudo sed -i "s/keexjEP3t4Mue23hrnuPtY4TdcsqNiJL-5174TsUdLmJSIXKfG2NGPwBL6vnRPddT7tH29qpkneX63DO9ECSPE9rzY1zhThHERg8lHM9IBFT+rVuiY823aQJuqzxCKIE1bcDqM4wgW01FH6oCBP1G4ub01xmb4BGSUG6ZrjxWHJyNLyIlGvOhoY2HAYzEtzYGwxFZn2JZ66o4RONkXjX0DF9EzsdUef3UAS+JQ+fCYReLawdjEe6tXCv88GKaaPKWxCeaUL9PejICQgRQOLGOZtZQkLgAelrOtehxz5ANOOqCaJgy2mJLQVLM5SJ9Dli909c5ybvEhVmIC0dc9dWH+/N9KmiLVlKMU7RJqnE+WXEEPI1SgglmfmLc1yVH7dqBb9ehOoKG9UE+HAE1YvH1XX2XVGeEqYUY-Tsk7YBTz0WpSpoYyPgx6Iki5KLtQ5G-aKP9eysnkuOAkrvHU8bLbGtZteGwJarev03PhfCioJL4OSqsmQGEvDbHFEbNl1qJtdwEriR+VNZts9vNNLk7UGfeNwIiqpxjk4Mn09nmSd8FhM4ifvcaIbNCRoMPGl6KU12iseSe+w+1kFsLhX+OhQM8WXcWV10cGqBzQE9OqOLUcg9n0krrR3KrohstS9smTwEx9olyLYppvC0p5i7dAx2deWvM1ZxKNs0BvcXGukR+/g" /usr/lib/beyondcompare/BCompare

Then restart BC, click "Enter License":

--- BEGIN LICENSE KEY ---
ayvZeJDYPBHS4J-1K6g6bDBuPoo0G-oGAq35blZtAoRqC-qQeSz28XAzX
6nTx9laTMLRCp6nAIhHNGZ2ehkeUfbnFaxEeLvI8fJavn-XQLNbOumCLU
qgdNbNMZiFRU03+OTQnw4V-E2YKTYi-LkgPzE6R-yIJGDNWfxH2AdpIgg
8rlpsbrTs9Dt1zysUfvAEi0dKbmGIi3rqf7yWmwDh1AI5VyoWFIejvJwJ
Lmlr2CjQ1VZ3DySCfBDuKcYmOCeK7jzEWPUnAw+f9360nIiiNEB0YGkwB
kdtgaKEEik7aNiI3jXvp5r34wViVJCiX7m2y7pqBV9gZIvP9hP9KPnP++++
--- END LICENSE KEY -----

#proxy on & off
# copy below functions to /etc/profile.d/proxy.sh
proxy_on() {
    proxy_off
    http_proxy="http://www-proxy-tyo.jp.oracle.com:80"
    no_proxy="localhost,127.0.0.1,$(hostname -f)"
    https_proxy=${http_proxy}
    ftp_proxy=${http_proxy}
    HTTP_PROXY=${http_proxy}
    HTTPS_PROXY=${http_proxy}
    FTP_PROXY=${http_proxy}
    NO_PROXY="${no_proxy}"

    cat <<EOF | sudo tee -a /etc/environment
http_proxy=${http_proxy}
https_proxy=${http_proxy}
ftp_proxy=${http_proxy}
no_proxy="${no_proxy}"
HTTP_PROXY=${http_proxy}
HTTPS_PROXY=${http_proxy}
FTP_PROXY=${http_proxy}
NO_PROXY="${no_proxy}"
EOF

if [[ -f ~/.ssh/config ]]; then
	echo 1
	if grep -q "Host *" ~/.ssh/config; then
		echo 2
  	  sed -i  '/Host \*/a\ \ ProxyCommand nc -X connect -x www-proxy-tyo.jp.oracle.com:80 %h %p' ~/.ssh/config
  	else
  		echo 3
  		cat <<EOF | tee -a ~/.ssh/config
Host *
  ProxyCommand nc -X connect -x www-proxy-tyo.jp.oracle.com:80 %h %p
  ServerAliveInterval   10
EOF
  	fi
else
	cat <<EOF | tee -a ~/.ssh/config
Host *
  ProxyCommand nc -X connect -x www-proxy-tyo.jp.oracle.com:80 %h %p
  ServerAliveInterval   10
EOF
fi

[[ ! -d ~/.gradle ]] && mkdir ~/.gradle
cat <<EOF |tee -a  ~/.gradle/gradle.properties
systemProp.http.proxyHost=www-proxy-tyo.jp.oracle.com
systemProp.http.proxyPort=80
systemProp.https.proxyHost=www-proxy-tyo.jp.oracle.com
systemProp.https.proxyPort=80
EOF

npm config set proxy $HTTP_PROXY
npm config set https-proxy $HTTPS_PROXY
npm config set strict-ssl false
echo "proxy = $HTTP_PROXY" > ~/.curlrc
echo "noproxy = $NO_PROXY" >> ~/.curlrc
git config --global http.proxy $HTTP_PROXY
git config --global https.proxy $HTTPS_PROXY

  [[ -d /etc/apt/apt.conf.d ]] && [[ -n ${http_proxy} ]] && cat <<EOF | sudo tee /etc/apt/apt.conf.d/01proxy
Acquire::http::proxy "${http_proxy}";
Acquire::https::proxy "${http_proxy}";
EOF

  gsettings set org.gnome.system.proxy mode 'auto'
  gsettings set org.gnome.system.proxy autoconfig-url 'http://wpad/dat'
}

proxy_off() {
  sudo sed -i "/^ftp_proxy/Id" /etc/environment
  sudo sed -i "/^http_proxy/Id" /etc/environment
  sudo sed -i "/^https_proxy/Id" /etc/environment
  sudo sed -i "/^no_proxy/Id" /etc/environment

  npm config delete proxy
  npm config delete "https-proxy"

  [[ -f ~/.curlrc ]] && sed -i "/^noproxy/Id" ~/.curlrc
  [[ -f ~/.curlrc ]] && sed -i "/^proxy/Id" ~/.curlrc
  [[ -f ~/.ssh/config ]] && sed -i "/^\s*ProxyCommand/Id" ~/.ssh/config
  [[ -f ~/.gradle/gradle.properties ]] && sed -i "/^systemProp.https\?.proxy/Id" ~/.gradle/gradle.properties

  git config --global --unset https.proxy
  git config --global --unset http.proxy

  [ -f /etc/apt/apt.conf.d/01proxy ] && sudo rm -f /etc/apt/apt.conf.d/01proxy



  unset ftp_proxy http_proxy https_proxy no_proxy FTP_PROXY HTTP_PROXY HTTPS_PROXY NO_PROXY

  gsettings set org.gnome.system.proxy mode 'none'
}

#support udisk type of exfat
sudo apt-get install -y exfat-utils

#initial git config
git config --global user.name lambert
git config --global user.email lamboyo@163.com

vpnconn() {
	/opt/cisco/anyconnect/bin/vpn status | grep -q "state: Connected"
	if [ $? -eq 0 ];  then
       echo "VPN is already connected! "
       return
	fi	
	/opt/cisco/anyconnect/bin/vpn connect 202.45.129.172
	/opt/cisco/anyconnect/bin/vpn status | grep -q "state: Connected"
	if [ $? -eq 0 ];  then
		echo "Connected! "
		proxy_on
	else
		echo "Connect attempt failed! "
	    proxy_off	
	fi
}

vpndisconn() {
	/opt/cisco/anyconnect/bin/vpn status | grep -q "state: Disconnected"
	if [ $? -eq 0 ];  then
		proxy_off
		echo "VPN is already disconnected! "
		return
	fi
	/opt/cisco/anyconnect/bin/vpn disconnect
	/opt/cisco/anyconnect/bin/vpn status | grep -q "state: Disconnected"
	if [ $? -eq 0 ];  then
		echo "Disconnected! "
	else
		echo "Failed to disconnect! "
	    proxy_off	
	fi
}

# 
sudo apt-get --purge -y remove linux-sound-base alsa-base alsa-utils
sudo apt-get install -y linux-sound-base alsa-base alsa-utils 
